home *** CD-ROM | disk | FTP | other *** search
- Path: Starbase.NeoSoft.COM!not-for-mail
- From: timd@Starbase.NeoSoft.COM (TimD)
- Newsgroups: comp.lang.c++
- Subject: Re: GCC and Default Constructors
- Date: 24 Jan 1996 17:10:52 -0600
- Organization: NeoSoft, Inc. +1 713 968 5800
- Message-ID: <4e6e9s$4so@Starbase.NeoSoft.COM>
- References: <4dtv71$1eh@news1.usa.pipeline.com> <1996Jan23.105906.1752@ittpub>
- NNTP-Posting-Host: starbase.neosoft.com
-
- In article <1996Jan23.105906.1752@ittpub>, Wil Evers <wil@ittpub.nl> wrote:
- >In article <4dtv71$1eh@news1.usa.pipeline.com> grantp@usa.pipeline.com
- >writes:
- >> On Jan 20, 1996 17:29:29 in article <GCC and Default Constructors>,
- >> 'Eric Richard <erichard@netgen.com>' wrote:
- >>
- >> >I just spent about 3 days [...]
- >> >
- >> >Basically my problem stemmed from a typo on my part and the fact
- >> >that C++ will automatically provide a default constructor for you if
- >> >you don't provide one. [...]
- >>
- >> >[...] what was screwing me up was that no
- >> >error was being generated because G++ was happily providing me with
- >> >a default constructor for my class.
-
- A very familiar problem. And sometimes very annoying. I think of
- this as an "unsafe" feature of C++. It can result in direct memory
- copies where you don't want them.
-
- >> >So, my question is this: Does anybody know if there is some way
- >> >of at least getting G++ to warn you if it is using a default
- >> >constructor? [...]
-
- If there were, it would be right there on the man page.
-
- >[...] as a
- >matter of routine, I always consider what should happen if someone tries
- >to copy an object of a class I wrote myself, and I make sure my classes
- >either have a defined copy constructor and assignment operator (even when
- >the compiler would have generated the same code), or I disable them by
- >declaring them private.
-
- Well, I'm not that paranoid--if a copy will work ok by direct memory
- copy or by calling copies of subclasses, I think it is okay. But
- very often I write those function prototypes with no bodies in a
- private part of the class. This means I will either get a compile
- or link error if they are used.
-
- >Sometimes, when in a paranoid mood, I use the following helper class:
- >
- > class NoGeneratedCopying {
- > private :
- > NoGeneratedCopying(const NoGeneratedCopying&);
- > void operator=(const NoGeneratedCopying&);
- > public :
- > NoGeneratedCopying() { }
- > };
- >
- >If you put a 'NoGeneratedCopying' data member in a class, the compiler is
- >not allowed to generate a copy constructor or assignment operator for that
- >class, and unless you explicitly provide them, an attempt to copy an
- >object of that class will result in a compile-time error.
-
- See, I didn't think of this! But making it a data member seems
- wrong from an intellectual perspective, I guess. It seems more
- pleasing to write:
-
- class MyClass : public ItsParent, public NoGeneratedCopying
- {
- ...
- }
-
- That way I can say
-
- MyClass is-a NoGeneratedCopying object!
-
- I don't think "virtual" is necessary 'cos there are no data members.
-
- Just a thought!
-
- -t
-